import {
  get,
  del
} from "../../utils/service"
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
const app = getApp()
// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: null
  },
  onDelete: function (event) {
    const d = event.detail
    if (!d) return
    const self = this

    Dialog.confirm({
        title: '确认',
        message: `确定要删除收件人[${d.receiverName}]吗?`,
      })
      .then(() => {
        const url = `${app.globalData.host}/receiver/${d.id}`
        console.info("===== delete recevier =====", url)
        del(url, {}, {
          success: function () {
            self.list()
          }
        })
      })
      .catch(() => {})
  },
  onEdit: function (data) {
    console.info(data, "------- edit------")
  },
  onSelect: function (data) {
    app.globalData.recevier = data.detail
    console.info(data.detail, "------------ address select------", app.globalData.recevier)
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  toAppend: function () {
    wx.navigateTo({
      url: '/pages/address/form/form',
    })
  },
  list: function () {
    const self = this
    get(`${app.globalData.host}/receiver`, {}, {
      success: function (data) {
        console.info("========== receiver ========", data)
        self.setData({
          data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.list()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})